package com.qf.card.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.card.dao.BankCardMapper;
import com.qf.card.pojo.BankCard;
import com.qf.card.pojo.Card;
import com.qf.card.service.BankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/****
 * @Author:shenkunlin
 * @Description:BankCard业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class BankCardServiceImpl implements BankCardService {

    @Autowired
    private BankCardMapper bankCardMapper;


    /**
     * BankCard条件+分页查询
     * @param bankCard 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<BankCard> findPage(BankCard bankCard, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(bankCard);
        //执行搜索
        return new PageInfo<BankCard>(bankCardMapper.selectByExample(example));
    }

    /**
     * BankCard分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<BankCard> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<BankCard>(bankCardMapper.selectAll());
    }

    /**
     * BankCard条件查询
     * @param bankCard
     * @return
     */
    @Override
    public List<BankCard> findList(BankCard bankCard){
        //构建查询条件
        Example example = createExample(bankCard);
        //根据构建的条件查询数据
        return bankCardMapper.selectByExample(example);
    }


    /**
     * BankCard构建查询对象
     * @param bankCard
     * @return
     */
    public Example createExample(BankCard bankCard){
        Example example=new Example(BankCard.class);
        Example.Criteria criteria = example.createCriteria();
        if(bankCard!=null){
            // 银行卡id
            if(!StringUtils.isEmpty(bankCard.getPkBankId())){
                    criteria.andEqualTo("pkBankId",bankCard.getPkBankId());
            }
            // 用户id
            if(!StringUtils.isEmpty(bankCard.getUserId())){
                    criteria.andEqualTo("userId",bankCard.getUserId());
            }
            // 银行卡账号
            if(!StringUtils.isEmpty(bankCard.getBankAccount())){
                    criteria.andEqualTo("bankAccount",bankCard.getBankAccount());
            }
            // 余额
            if(!StringUtils.isEmpty(bankCard.getBalance())){
                    criteria.andEqualTo("balance",bankCard.getBalance());
            }
            // 状态(正常:1/冻结0)（2：申请开户)
            if(!StringUtils.isEmpty(bankCard.getBranchState())){
                    criteria.andEqualTo("branchState",bankCard.getBranchState());
            }
            // 每天最大限额
            if(!StringUtils.isEmpty(bankCard.getQuota())){
                    criteria.andEqualTo("quota",bankCard.getQuota());
            }
            // 银行卡密码
            if(!StringUtils.isEmpty(bankCard.getBankPwd())){
                    criteria.andEqualTo("bankPwd",bankCard.getBankPwd());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        bankCardMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改BankCard
     * @param bankCard
     */
    @Override
    public void update(BankCard bankCard){
        bankCardMapper.updateByPrimaryKey(bankCard);
    }

    /**
     * 增加BankCard
     * @param bankCard
     */
    @Override
    public void add(BankCard bankCard){
        bankCardMapper.insert(bankCard);
    }

    /**
     * 根据ID查询BankCard
     * @param id
     * @return
     */
    @Override
    public BankCard findById(Integer id){
        return  bankCardMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询BankCard全部数据
     * @return
     */
    @Override
    public List<BankCard> findAll() {
        return bankCardMapper.selectAll();
    }

    @Override
    public List<Card> showCards(Integer userId) {

        //根据user_id查询银行卡
        List<BankCard> bankCards = bankCardMapper.selectCardsByUserId(userId);

        ArrayList<Card> cards = new ArrayList();

        for (BankCard bankCard : bankCards) {
            System.out.println(bankCard);

            Card card = new Card();
            card.setUserName("伍甄梅");
            card.setBankAccount(bankCard.getBankAccount());
            card.setBalance(bankCard.getBalance());
            card.setBranchState(bankCard.getBranchState());
            card.setQuota(bankCard.getQuota());
            cards.add(card);
        }

        return cards;
    }

    //银行卡解冻
    @Override
    public void bankCardUnFreeze(Integer id) {
        bankCardMapper.bankCardUnFreeze(id);
    }

    //银行卡冻结
    @Override
    public void bankCardFreeze(Integer id) {
        bankCardMapper.bankCardFreeze(id);
    }
}
